package jskj.ekt;

import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.rpc.service.EchoService;
import jskj.ekt.api.dubbo.IDemoClinet;
import jskj.ekt.api.dubbo.IDemoService;
import jskj.ekt.api.dubbo.PushTest;
import jskj.ekt.config.PushBean;
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述:
 * ${DESCRIPTION}
 *
 * @author JSKJ
 * @create 2019-01-02 11:40
 */
public class main {

    private static Logger log = Logger.getLogger(main.class);
    public static void main(String[] args) throws Exception {
//        http://10.20.160.198/wiki/display/dubbo/provider.xml
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"/applicationContext.xml"});
        context.start();


        log.info("服务已启动");
        log.debug("debug服务已启动");
        log.error("error服务已启动");

        System.in.read(); // 按任意键退出



//        String beanId = "192.168.1.197:20881/jskj.ekt.api.dubbo.IDemoClinet";
//        Object bean = context.getBean(beanId);
//        System.out.println(bean);

        String url = "dubbo://192.168.1.197:20881/jskj.ekt.api.dubbo.PushTest";//更改不同的Dubbo服务暴露的ip地址&端口
        String url2 = "dubbo://192.168.1.197:20882/jskj.ekt.api.dubbo.PushTest";//更改不同的Dubbo服务暴露的ip地址&端口


        PushBean<PushTest> pushTestPushBean = new PushBean<>(url, context);
        PushTest pushBean = pushTestPushBean.getPushBean(PushTest.class);
        System.out.println(pushBean.pushOrder("test order"));

        ReferenceBean<PushTest> referenceBean = new ReferenceBean<PushTest>();
        referenceBean.setApplicationContext(context);
        referenceBean.setInterface(jskj.ekt.api.dubbo.PushTest.class);
        referenceBean.setUrl(url);
//        referenceBean.setId(beanId);

        try {
            referenceBean.afterPropertiesSet();
            PushTest demoService = referenceBean.get();
            System.out.println(demoService.pushOrder("Tester  指定 url"));
            System.out.println("外 ****"+referenceBean.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }


        Map<String,PushTest> map = new HashMap<>();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        do {
            String read = br.readLine();
            if(read.equals("1")){

                if(!map.containsKey(url)) {

                    PushBean<PushTest> pushTestPushBean1 = new PushBean<>(url, context);
                    PushTest pushBean1 = pushTestPushBean1.getPushBean(PushTest.class);
                    System.out.println(pushBean1.pushOrder("我是1 test order"));

                    map.put(url,pushBean1);
                }else {
                    System.out.println(map.get(url).pushOrder("1"));
                }
            }
            if(read.equals("2")){
                PushBean<PushTest> pushTestPushBean2 = new PushBean<>(url2, context);
                PushTest pushBean2 = pushTestPushBean2.getPushBean(PushTest.class);
                System.out.println(pushBean2.pushOrder("我是2 test order"));
            }

            if(true){
                continue;
            }


            IDemoClinet demoClient = (IDemoClinet) context.getBean("demoClient"); // 获取远程服务代理
            String hello = demoClient.sayHello2("world7777"); // 执行远程方法
            System.out.println(hello); // 显示调用结果
//            System.in.read(); // 按任意键退出

//            IDemoClinet demoClientt = (IDemoClinet) context.getBean("demoClientt"); // 获取远程服务代理
            IDemoClinet demoClientt = (IDemoClinet) context.getBean("demoClient"); // 获取远程服务代理
            hello = demoClientt.sayHello2("我是C兔"); // 执行远程方法
            System.out.println(hello); // 显示调用结果
//            System.in.read(); // 按任意键退出

// 远程服务引用

            EchoService echoService = (EchoService) context.getBean("demoClient"); // 强制转型为EchoService

// 回声测试可用性
            Object status = echoService.$echo("OK");

            assert(status.equals("OK"));


        }while (true);
    }
}
